// pages/pro/pro.js
import request from '../../utils/request'
Page({
    /**
     * 页面的初始数据
     */
    data: {
      proInfo: {},
      imgs: [],
      shopId: 1,
      indicatorDots: true,
      indicatorColor: '#f2f2f2',
      indicatorActiveColor: '#eb2444',
      autoplay: true,
      interval: 4000,
      duration: 2000,
      prodNum: 1,
      totalCartNum: 0,
      pic: "",
      prodName: '',
      price: 0,
      content: '',
      proId: 1,
      brief: '',
      skuId: 0,
      popupShow: false,
      // 是否获取过用户领取过的优惠券id
      loadCouponIds: false,
      skuShow: false,
      commentShow: false,
      couponList: [],
      skuList: [],
      skuGroup: {},
      findSku: true,
      selectedProp: [],
      selectedPropObj: {},
      propKeys: [],
      allProperties: [],
      prodCommData: {},
      prodCommPage: {
        current: 0,
        pages: 0,
        records: []
      },
      time: "",
      littleCommPage: [],
      evaluate: -1,
      isCollection: false
    },
  
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
      this.setData({
        proId: options.id
      })
      this.getProdInfo(options.id);
      // 加载评论数据
      // this.getProdCommData();
      // 加载评论项
      // this.getLittleProdComm();
      // 查看用户是否关注
      // this.getCollection();
    },
  
    /**
     * 获取是否关注信息
     */
    bindTimeChange(e){
      this.setData({
        time: e.detail.value
      })
    },
    /**
     * 添加或者取消收藏商品 
     */
    // 获取商品信息
    async getProdInfo(proId) {
     //  wx.showLoading();
      let proData = await request("/cli/showpro",{id:proId})
      this.setData({
            proInfo: proData.data[0],
            imgs: proData.data[0].proUrl
      });
    },
    getProdCommData() {
      http.request({
        url: "/prodComm/prodCommData",
        method: "GET",
        data: {
          proId: this.data.proId,
        },
        callBack: (res) => {
          this.setData({
            prodCommData: res
          })
        }
      })
    },
    // 获取部分评论
    getLittleProdComm() {
      if (this.data.prodCommPage.records.length) {
        return;
      }
      // this.getProdCommPage();
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {
  
    },
  
    /**
     * 生命周期函数--监听页面显示
     */
    // onShow: function() {
    //   this.setData({
    //     totalCartNum: app.globalData.totalCartCount
    //   });
    // },
  
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {
  
    },
  
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {
  
    },
  
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
  
    },
  
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
  
    },
  
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {
  
    },
  
    /**
     * 跳转到首页
     */
    toHomePage: function() {
      wx.switchTab({
        url: '/pages/index/index',
      })
    },
  
    /**
     * 跳转到购物车
     */
    toCartPage: function() {
      wx.switchTab({
        url: '/pages/basket/basket',
      })
    },
  
    /**
     * 取消
     */
    cancel: function() {
      this.setData({
        skuShow: false
      });
    },
  
    /**
     * 立即预定
     */
    buyNow: function() {
      if (!this.data.findSku) {
        return;
      }
      if(this.data.time == ""){
        wx.showToast({
          title: '请选择时间',
          icon: 'error'
        })
        return;
      }
      // wx.setStorage('orderItem', null)
      wx.setStorageSync("orderItem", JSON.stringify({
        proId: this.data.proId,
        skuId: this.data.skuId,
        time: this.data.time,
        prodCount: this.data.prodNum,
        shopId: this.data.shopId
      }));
      wx.reLaunch({
        url: '/pages/cart/cart',
      })
      wx.showToast({
        title: '加入购物车成功',
      })
    },
  
    /**
     * 减数量
     */
    onCountMinus: function() {
      var prodNum = this.data.prodNum;
      if (prodNum > 1) {
        this.setData({
          prodNum: prodNum - 1
        });
      }
    },
  
    /**
     * 加数量
     */
    onCountPlus: function() {
      var prodNum = this.data.prodNum;
      if (prodNum < 1000) {
        this.setData({
          prodNum: prodNum + 1
        });
      }
    },
  
    /**
     * 分享设置
     */
    onShareAppMessage: function(res) {
      return {
        title: this.data.prodName,
        path: '/pages/prod/prod?proId=' + this.data.proId
      }
    },
  
    showPopup: function() {
      if (this.data.loadCouponIds) {
        this.setData({
          popupShow: true
        });
        return;
      }
      http.request({
        url: "/p/myCoupon/listCouponIds",
        method: "GET",
        data: {},
        callBack: (couponIds) => {
          var couponList = this.data.couponList;
  
          couponList.forEach(coupon => {
            if (couponIds && couponIds.length) {
              // 领取该优惠券数量
              var couponLimit = 0;
              couponIds.forEach(couponId => {
                if (couponId == coupon.couponId) {
                  couponLimit++;
                }
              });
              // 小于用户领取优惠券上限，可以领取优惠券
              if (couponLimit < coupon.limitNum) {
                coupon.canReceive = true;
              } else {
                coupon.canReceive = false;
              }
            } else {
              coupon.canReceive = true;
            }
          });
          this.setData({
            couponList: couponList,
            popupShow: true,
            loadCouponIds: true
          })
        }
      })
    },
    showSku: function() {
      this.setData({
        skuShow: true
      });
    },
    showComment: function() {
      this.setData({
        commentShow: true
      });
    },
  
    closePopup: function() {
      this.setData({
        popupShow: false,
        skuShow: false,
        commentShow: false
      });
    },
  
  
  
  })